package com.mobiporter.adias.tags.utils;

import java.util.List;

import com.mobiporter.adias.tags.model.LabeledComponentModel;
import com.mobiporter.adias.tags.model.SelectItem;

public class HTMLTagGenerator 
{
	public static String getSelectBox(List<SelectItem> options, String name,String valueSelected)
	{
		String tagName = "select";
		name = getName(tagName,name);
		
		TagBuilder builder = TagBuilder.newInstance(tagName);
		String innerHTML = null ;
		if (!(options==null || options.isEmpty()))
		{
			for (SelectItem m : options)
			{
				boolean selected = false ;
				if (m.getValue().equals(valueSelected))
				{
					selected = true ;
				}
				innerHTML += getOption(m.getKey(),m.getValue(),selected);
			}
		}
		builder.setInnerHTML(innerHTML) ;
		return builder.toString() ;
	}
	
	private static String getOption(String name, String value,boolean selected) 
	{
		TagBuilder tagBuilder = TagBuilder.newInstance("option") ;
		tagBuilder.addAttribute("name", value);
		tagBuilder.addAttribute("id", name);
		if (selected)
		{
			tagBuilder.addAttribute("selected", "selected");
		}
		tagBuilder.setInnerHTML(name);
		return tagBuilder.toString() ;
	}

	private static String getName(String tagName,String name) 
	{
		if (name==null || name.trim().equals(""))
		{
			name = tagName+"Box"; 
			return name ;
		}
		else
			return name ;
	}

	public static String getTextBox(String id,String defaultText) 
	{
		TagBuilder tagBuilder = TagBuilder.newInstance("input");
		tagBuilder.addAttribute("type", "text");
		tagBuilder.addAttribute("id", id);
		tagBuilder.addAttribute("value", defaultText);
		return tagBuilder.toString() ;
	} 
	
	public static String getLink(String id,String text,String href)
	{
		TagBuilder tagBuilder = TagBuilder.newInstance("a");
		tagBuilder.addAttribute("id", id);
		tagBuilder.setInnerHTML(text);
		tagBuilder.addAttribute("href", href);
		return tagBuilder.toString() ;
	}

	public static String getRadioButton(String id,String label, String value) 
	{
		TagBuilder tagBuilder = TagBuilder.newInstance("input");
		tagBuilder.addAttribute("type", "radio");
		tagBuilder.addAttribute("name", id);
		tagBuilder.addAttribute("value", value);
		
		return tagBuilder.toString()+ label;
	}

	public static String getBreakLine() 
	{
		return "<br/>" ;
	}

	public static String getHiddenField(String id, String label, String value) 
	{
		TagBuilder builder = TagBuilder.newInstance("input");
		builder.addAttribute("type", "hidden");
		builder.addAttribute("id", id);
		builder.addAttribute("name", id);
		builder.addAttribute("value", value);
		return builder.toString(); 
	}

	public static String getDiv(String innerText, String className) {
		TagBuilder builder = TagBuilder.newInstance("div");
		builder.addAttribute("class", className);
		builder.setInnerHTML(innerText);
		return builder.toString();
	}
	
	public static String getTabledLabels(List<LabeledComponentModel> labelComponents, String tdClass)
	{
		TagBuilder tableBuilder = TagBuilder.newInstance("table");
		
		String innerHTML = "" ;
		for (LabeledComponentModel label : labelComponents)
		{
			String tempHTML = "" ;
			TagBuilder trBuilder = TagBuilder.newInstance("tr");
			TagBuilder tdBuilder = TagBuilder.newInstance("td");
			tdBuilder.addAttribute("class", tdClass);
			tdBuilder.setInnerHTML(label.getText()+":");
			tempHTML = tdBuilder.toString() ;
			tdBuilder.setInnerHTML(label.getValue());
			tempHTML += tdBuilder.toString() ;
			trBuilder.setInnerHTML(tempHTML);
			innerHTML+= trBuilder.toString() ;
		}
		
		tableBuilder.setInnerHTML(innerHTML);
		return tableBuilder.toString() ;
	}
}
